package cn.kxx.controller;

import cn.kxx.constant.MessageConstant;
import cn.kxx.entity.PageResult;
import cn.kxx.entity.QueryPageBean;
import cn.kxx.entity.Result;
import cn.kxx.pojo.CheckItem;
import cn.kxx.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 体检检查项管理
 */
@Api(tags = "检查项管理模块")
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;


    //新增
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    @ApiOperation(value = "添加检查项")
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        checkItemService.add(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //分页查询
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    @ApiOperation(value = "分页查询检查项")
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page= checkItemService.findPage(queryPageBean);
        return page;
    }
    //删除体检项
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @ApiOperation(value = "删除体检项")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){

        checkItemService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    //编辑回显数据
    @ApiOperation(value = "编辑回显数据")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id){

       CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,checkItem);
    }
    //修改体检项
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    @ApiOperation(value = "修改体检项")
    @PutMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){

       checkItemService.edit(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    //查找所有检查项

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    @ApiOperation(value = "查找所有检查项")
    @GetMapping("/findAll")
    public Result findAll(){

      List<CheckItem> list = checkItemService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

}
